package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.TravelGroupService;
import com.atguigu.vo.PageResult;
import com.atguigu.vo.QueryPageBean;
import com.atguigu.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-14 14:00
 **/
@RestController
@RequestMapping("/travelGroup")
public class TravelGroupController {

    @Reference
    TravelGroupService travelGroupService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = travelGroupService.findPage
                (queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 添加
     *
     * @param travelItemIds
     * @param travelGroup
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestParam("travelItemIds") Integer[] travelItemIds, @RequestBody TravelGroup travelGroup) {
        try {
            travelGroupService.add(travelItemIds, travelGroup);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELGROUP_FAIL);
        }
    }

    /**
     * 添加
     *
     * @param travelItemIds
     * @param travelGroup
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestParam("travelItemIds") Integer[] travelItemIds, @RequestBody TravelGroup travelGroup) {
        try {
            travelGroupService.update(travelItemIds, travelGroup);
            return new Result(true, MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findByID(@RequestParam("id") Integer id) {
        try {
            TravelGroup travelGroup = travelGroupService.findById(id);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS, travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    /**
     * 查询回显数据  所选中的
     *
     * @param id
     * @return
     */
    @GetMapping("/findTravelItemIdByTravelgroupId")
    public Result findTravelItemIdByTravelgroupId(@RequestParam("id") Integer id) {
        try {
            List<Integer> travelItemIds = travelGroupService.findTravelItemIdByTravelgroupId(id);
            return new Result(true, MessageConstant.QUERY_TRAVELITEM_SUCCESS, travelItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELITEM_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        try {
            travelGroupService.delete(id);
            return new Result(true, MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("/findAll")
    public Result findAll() {
        try {
            List<TravelGroup> list = travelGroupService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }


    }
}
